Public Class EditRuntimes

    Private _newInstalledRuntimes As New System.Collections.ObjectModel.Collection(Of String)
    Private _deletedInstalledRuntimes As New System.Collections.ObjectModel.Collection(Of Integer)

    Private _fileId As Integer

    Public Event InstalledRuntimesChanged()

    Private Sub AddRuntime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddRuntime.Click

        RuntimeSelector.ShowDialog()
        Dim selectedRuntime As String = RuntimeSelector.SelectedPath

        Dim newRuntime As InstalledRuntimesData.InstalledRuntimesRow = InstalledRuntimesData.InstalledRuntimes.NewInstalledRuntimesRow
        newRuntime.file_id = _fileId
        newRuntime.runtime = selectedRuntime
        InstalledRuntimesData.InstalledRuntimes.AddInstalledRuntimesRow(newRuntime)
        InstalledList.Refresh()

        _newInstalledRuntimes.Add(selectedRuntime)

    End Sub

    Private Sub RemoveRuntime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveRuntime.Click

        Dim entryRow As InstalledRuntimesData.InstalledRuntimesRow
        Dim rowsToDelete As New System.Collections.ObjectModel.Collection(Of InstalledRuntimesData.InstalledRuntimesRow)

        For Each entry As System.Data.DataRowView In InstalledList.SelectedItems
            entryRow = CType(entry.Row, InstalledRuntimesData.InstalledRuntimesRow)
            If entryRow.RowState = DataRowState.Added Then
                _newInstalledRuntimes.Remove(entryRow.runtime)
            Else
                _deletedInstalledRuntimes.Add(entryRow.installed_id)
            End If
            rowsToDelete.Add(entryRow)
        Next

        For Each entryRow In rowsToDelete
            InstalledRuntimesData.InstalledRuntimes.RemoveInstalledRuntimesRow(entryRow)
        Next

        InstalledList.Refresh()

    End Sub

    Private Sub EditRuntimes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Public Sub New(ByVal fileId As Integer)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _fileId = fileId

        Me.InstalledRuntimesTableAdapter.Fill(Me.InstalledRuntimesData.InstalledRuntimes, _
                                              fileId)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        InstalledDS.CancelEdit()
        Me.Close()

    End Sub

    Private Sub UpdateFileDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateFileDetails.Click

        For Each deletedRuntime As Integer In _deletedInstalledRuntimes
            Me.InstalledRuntimesTableAdapter.Delete(deletedRuntime)
        Next
        InstalledDS.EndEdit()
        Dim updated As Integer = Me.InstalledRuntimesTableAdapter.Update(Me.InstalledRuntimesData)

        RaiseEvent InstalledRuntimesChanged()

        Me.Close()

    End Sub

End Class